﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using RMS.Utils.Dicom.Commen;

namespace RMS.Utils.Dicom.DataStructure
{
    public class VR
    {
        #region [Properties]
        private int ByteLenght = Constants.TageByteLenght;
        private Tag tag;

        private string _name = null;
        public string Name
        {
            get
            {
                return _name;
            }
        }
        public bool IsImplicit
        {

            get
            {
                return (tag.TagName.Equals(Constants.SequenceDelimitation)) ||
                    (tag.TagName.Equals(Constants.ItemDelimitation)) ||
                    (tag.TagName.Equals(Constants.Item)) || TransferSyntax.IsImplicitVR;
            }
        }
        #endregion
        #region [Constructor]
        public VR(Tag tag)
        {
            this.tag = tag;
            ReadVR();
        }
        #endregion
        #region [HelperMethod]
        private void ReadVR()
        {
            if (tag.IsUserDefine)
                _name = "UN";
            else if (this.IsImplicit)
                _name = "No VR";
            else
                CheckVR(StreamFile.Read(ByteLenght).GetString());
        }
        private void CheckVR(string vr)
        {
            if (Regex.IsMatch(vr, "^(AE|AS|AT|CS|DA|DS|DT|FL|FD|IS|LO|LT|OB|OW|OF|SQ|PN|SH|SL|SS|ST|TM|UI|UL|UN|US|UT)$"))
                _name = vr.Trim().ToUpper();
            else
                throw new Exception("Inviled Dicom VR.");
        }
        #endregion
    }
}
